package com.jkt.rms.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.ModelAndView;

import com.jkt.rms.businessservice.VendorBusinessService;
import com.jkt.rms.cache.RMSCache;
import com.jkt.rms.commons.utils.RMSException;
import com.jkt.rms.commons.utils.ServiceLocator;
import com.jkt.rms.vo.VendorVO;

public class VendorController extends AbstractMultiActionController {

	Logger logger = Logger.getLogger(VendorController.class);
	private VendorBusinessService vendorBusinessService;
	private RMSCache cache;
	/**
	 * add the vendor 
	 * @param request
	 * @param response
	 * @param vendorVO
	 * @return
	 * @throws RMSException
	 */
	public ModelAndView addVendor(HttpServletRequest request,HttpServletResponse response, VendorVO vendorVO) {
		boolean isVendorExist = false;
		VendorVO vendorVOWithBlankfield = new VendorVO();
		getBusinessService();
		validate(vendorVO);
		if (errors != null && errors.getAllErrors() != null
				&& errors.getAllErrors().size() > 0) {
			return new ModelAndView("vendorlist", "vendorData", vendorVO).addObject("errors", errors);
		} else {
			try {
				isVendorExist = vendorBusinessService.saveVendor(vendorVO);
				getCache();
				cache.refersh(RMSCache.VENDORCACHE);
				
			} catch (RMSException e) {
				logger.error("Error Occurs in save Vendor :", e.getCause());
			}
			if (isVendorExist) {
				vendorVO.setMessage("vendor.already.exists");
			}else {
				vendorVOWithBlankfield.setMessage("vendor.create.success.msg");
			}
			return new ModelAndView("vendorlist", "vendorData", vendorVOWithBlankfield);
		}

	}
	
	public ModelAndView updateVendor(HttpServletRequest request,HttpServletResponse response, VendorVO vendorVO) {
		boolean isVendorExist = false;
		getBusinessService();
		validate(vendorVO);
		if (errors != null && errors.getAllErrors() != null
				&& errors.getAllErrors().size() > 0) {
			return new ModelAndView("updateVendor", "vendorData", vendorVO).addObject("errors", errors);
		} else {
			try {
				isVendorExist = vendorBusinessService.updateVendor(vendorVO);
				getCache();
				cache.refersh(RMSCache.VENDORCACHE);
			} catch (RMSException e) {
				logger.error("Error Occurs in save Vendor :", e.getCause());
			}
			if (isVendorExist) {
				vendorVO.setMessage("updated.nameCode.exists");
			}else {
				vendorVO.setMessage("vendor.update.success.msg");
			}
			return new ModelAndView("updateVendor", "vendorData", vendorVO);
		}

	}

	/**
	 * This method search all the result of vendor
	 * @param request
	 * @param response
	 * @return
	 * @throws RMSException
	 */
	public ModelAndView findAllVendor(HttpServletRequest request,HttpServletResponse response)throws RMSException {
		getBusinessService();
		VendorVO vendorVO =  new VendorVO();
		List<VendorVO> listOfVendorVO = vendorBusinessService.findAllVendors();
		if(listOfVendorVO == null || listOfVendorVO.size() == 0){
			vendorVO.setMessage("no.record.found");
			return new ModelAndView("updateVendor", "vendorData", vendorVO);
		} else {
			vendorVO.setSearchMethod("findAllVendor");
		}
		return new ModelAndView("vendorlist", "vendorData", vendorVO).addObject("searchResult", listOfVendorVO);
	}
	 
	 /**
     * This method search the vendor based on search criteria.
     * @param request
     * @param response
     * @param vendorVO
     * @return
     * @throws RMSException
     */
	public ModelAndView searchVendor(HttpServletRequest request, HttpServletResponse response, VendorVO vendorVO){
		getBusinessService();
		VendorVO requestSerachVendorVO=new VendorVO();
		requestSerachVendorVO.setVendorCode(vendorVO.getSearchVendorCode());
		requestSerachVendorVO.setVendorName(vendorVO.getSearchVendorName());
		List<VendorVO> listOfVendorVO = vendorBusinessService.findByCriteria(requestSerachVendorVO);
		VendorVO responseSerachVendorVO=new VendorVO();
		responseSerachVendorVO.setSearchVendorCode(vendorVO.getSearchVendorCode());
		responseSerachVendorVO.setSearchVendorName(vendorVO.getSearchVendorName());
		if(listOfVendorVO == null ) {
			responseSerachVendorVO.setMessage("criteria.not.entered");
		} else if (listOfVendorVO.size() == 0){
			responseSerachVendorVO.setMessage("no.record.found");
		}
		responseSerachVendorVO.setSearchMethod("searchVendor");
		return new ModelAndView("vendorlist", "vendorData", responseSerachVendorVO).addObject("searchResult", listOfVendorVO);
	}

	/**
	 * This method load the vendor Page.
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView loadVendor(HttpServletRequest request, HttpServletResponse response) throws Exception {
		VendorVO vendorVO =  new VendorVO();
		return new ModelAndView("vendorlist", "vendorData", vendorVO);
	}
	
	
		/**
	 * This method load the edit vendor Page.
	 * @param request
	 * @param response
	 * @return ModelAndView
	 * @throws Exception
	 */
	public ModelAndView loadEditVendor(HttpServletRequest request,
			HttpServletResponse response,VendorVO vendorVO) throws Exception {
		String searchMethod=vendorVO.getSearchMethod();
		String searchVendorName=vendorVO.getSearchVendorName();
		String searchVendorCode=vendorVO.getSearchVendorCode();
		getCache();
		vendorVO=cache.getVendor(vendorVO.getVendorName());
		vendorVO.setSearchMethod(searchMethod);
		vendorVO.setSearchVendorName(searchVendorName);
		vendorVO.setSearchVendorCode(searchVendorCode);
		return new ModelAndView("updateVendor","vendorData",vendorVO);
	}
	
	/**
	 * Setter method to set business service.
	 */
	private void getBusinessService() {
		vendorBusinessService = ServiceLocator.getServiceLocator().locate("vendorBusinessService");
	}
	
	private RMSCache getCache() {
		cache = ServiceLocator.getServiceLocator().locate("cacheService");
		return cache;
	}
}
